package com.jebel.framework.classic.event.worker;

import com.jebel.framework.classic.event.service.ICommonRestService;
import com.jebel.framwork.common.response.RespBodyBuilder;
import com.jebel.framwork.common.response.RespCode;
import com.jebel.framwork.common.util.StringUtil;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.rx.java.RxHelper;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import rx.functions.Action0;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class ClassicEventVerticle extends AbstractVerticle {

    private static Logger logger = LoggerFactory.getLogger(ClassicEventVerticle.class);

    @Autowired
    ICommonRestService commonRestService;

    public void start() throws Exception {

        RxHelper.scheduler(vertx).createWorker().schedulePeriodically(new Action0() {
            @Override
            public void call() {
//
//                vertx.eventBus().send("classic.event.seasons", null);
//                vertx.eventBus().send("classic.event.season", new JsonObject().put("seasonHref", "https://cuetracker.net/Seasons/2019-2020"));
//                vertx.eventBus().send("classic.event.tournament", new JsonObject().put("tournamentHref", "https://cuetracker.net/tournaments/gibraltar-open/2020/3542"));


                List list = new ArrayList<>();
                //championship-league
//                list.add("https://cuetracker.net/tournaments/championship-league/2008/137");
//                list.add("https://cuetracker.net/tournaments/championship-league/2009/101");
//                list.add("https://cuetracker.net/tournaments/championship-league/2010/72");
//                list.add("https://cuetracker.net/tournaments/championship-league/2011/12");
//                list.add("https://cuetracker.net/tournaments/championship-league/2012/262");
//                list.add("https://cuetracker.net/tournaments/championship-league/2013/557");
//                list.add("https://cuetracker.net/tournaments/championship-league/2014/1005");
//                list.add("https://cuetracker.net/tournaments/championship-league/2015/1077");
//                list.add("https://cuetracker.net/tournaments/championship-league/2016/1350");
//                list.add("https://cuetracker.net/tournaments/championship-league/2017/2262");
//                list.add("https://cuetracker.net/tournaments/championship-league/2018/2622");
//                list.add("https://cuetracker.net/tournaments/championship-league/2019/2820");
//                list.add("https://cuetracker.net/tournaments/championship-league/2020/3880");
//                list.add("https://cuetracker.net/tournaments/championship-league/2020/3650");
//                list.add("https://cuetracker.net/tournaments/championship-league/2020/3275");
//                list.add("https://cuetracker.net/tournaments/championship-league/2021/4295");

                //BRITISH-OPEN
                list.add("HTTPS://CUETRACKER.NET/TOURNAMENTS/BRITISH-OPEN/2004/219");
                list.add("HTTPS://CUETRACKER.NET/TOURNAMENTS/BRITISH-OPEN/2003/236");
                list.add("HTTPS://CUETRACKER.NET/TOURNAMENTS/BRITISH-OPEN/2002/253");
                list.add("HTTPS://CUETRACKER.NET/TOURNAMENTS/BRITISH-OPEN/2001/279");
                list.add("HTTPS://CUETRACKER.NET/TOURNAMENTS/BRITISH-OPEN/2000/303");
                list.add("HTTPS://CUETRACKER.NET/TOURNAMENTS/BRITISH-OPEN/1999/324");
                list.add("HTTPS://CUETRACKER.NET/TOURNAMENTS/BRITISH-OPEN/1999/329");
                list.add("HTTPS://CUETRACKER.NET/TOURNAMENTS/BRITISH-OPEN/1998/346");
                list.add("HTTPS://CUETRACKER.NET/TOURNAMENTS/BRITISH-OPEN/1997/369");
                list.add("HTTPS://CUETRACKER.NET/TOURNAMENTS/BRITISH-OPEN/1996/399");
                list.add("HTTPS://CUETRACKER.NET/TOURNAMENTS/BRITISH-OPEN/1995/428");
                list.add("HTTPS://CUETRACKER.NET/TOURNAMENTS/BRITISH-OPEN/1994/449");
                list.add("HTTPS://CUETRACKER.NET/TOURNAMENTS/BRITISH-OPEN/1993/470");
                list.add("HTTPS://CUETRACKER.NET/TOURNAMENTS/BRITISH-OPEN/1992/492");
                list.add("HTTPS://CUETRACKER.NET/TOURNAMENTS/BRITISH-OPEN/1991/514");
                list.add("HTTPS://CUETRACKER.NET/TOURNAMENTS/BRITISH-OPEN/1990/543");
                list.add("HTTPS://CUETRACKER.NET/TOURNAMENTS/BRITISH-OPEN/1989/562");
                list.add("HTTPS://CUETRACKER.NET/TOURNAMENTS/BRITISH-OPEN/1988/611");
                list.add("HTTPS://CUETRACKER.NET/TOURNAMENTS/BRITISH-OPEN/1987/633");
                list.add("HTTPS://CUETRACKER.NET/TOURNAMENTS/BRITISH-OPEN/1986/680");
                list.add("HTTPS://CUETRACKER.NET/TOURNAMENTS/BRITISH-OPEN/1985/702");

                list.stream().forEach(t -> {
                    vertx.eventBus().send("classic.event.britishOpen", new JsonObject().put("tournamentHref", t));
                });

            }
        }, 5, 10000, TimeUnit.SECONDS);


        vertx.eventBus().consumer("classic.event.britishOpen", message -> {
            JsonObject param = ((JsonObject) message.body());
            logger.info("param:{}", param);
            String result = RespBodyBuilder.toError(RespCode.CODE_10500);
            try {
                String data = commonRestService.getForObject(param.getString("tournamentHref"));
                JsonObject tournament = new JsonObject();
                tournament.put("tournamentHref", param.getString("tournamentHref"));
                String tournamentId = param.getString("tournamentHref").split("/")[6];
                tournament.put("tournamentId", tournamentId);
                if (!StringUtil.isEmpty(data)) {
                    Document document = Jsoup.parse(data);
//                    logger.info("{}", document.html());
                    String tournamentName = document.getElementsByClass("page-content").first().child(0).text();
                    tournament.put("tournamentName", tournamentName);
                    Elements tournamentDescs = document.getElementsByClass("page-content").first().child(2).child(0).getElementsByTag("tbody").first().children();
                    String tournamentStatus = tournamentDescs.get(0).child(1).text();
                    tournament.put("tournamentStatus", tournamentStatus);
                    String tournamentSeason = tournamentDescs.get(1).child(1).text();
                    tournament.put("tournamentSeason", tournamentSeason);
                    String tournamentDates = tournamentDescs.get(2).child(1).text();
                    tournament.put("tournamentDates", tournamentDates);

                    JsonArray round = new JsonArray();
                    Elements rounds = document.getElementsByClass("page-content").first().child(2).child(1).getElementsByClass("match");

                    XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
                    XSSFSheet level3Sheet = xssfWorkbook.createSheet(tournamentName);
                    XSSFRow level3SheetRow = level3Sheet.createRow(0);
                    XSSFCell level3SheetXssfCell = level3SheetRow.createCell(0);
                    level3SheetXssfCell.setCellValue("season");
                    level3SheetXssfCell = level3SheetRow.createCell(1);
                    level3SheetXssfCell.setCellValue("name");
                    level3SheetXssfCell = level3SheetRow.createCell(2);
                    level3SheetXssfCell.setCellValue("status");
                    level3SheetXssfCell = level3SheetRow.createCell(3);
                    level3SheetXssfCell.setCellValue("date");
                    level3SheetXssfCell = level3SheetRow.createCell(4);
                    level3SheetXssfCell.setCellValue("round-name");
                    level3SheetXssfCell = level3SheetRow.createCell(5);
                    level3SheetXssfCell.setCellValue("player1name");
                    level3SheetXssfCell = level3SheetRow.createCell(6);
                    level3SheetXssfCell.setCellValue("player1score");
                    level3SheetXssfCell = level3SheetRow.createCell(7);
                    level3SheetXssfCell.setCellValue("all0score");
                    level3SheetXssfCell = level3SheetRow.createCell(8);
                    level3SheetXssfCell.setCellValue("player2score");
                    level3SheetXssfCell = level3SheetRow.createCell(9);
                    level3SheetXssfCell.setCellValue("player2name");
                    level3SheetXssfCell = level3SheetRow.createCell(10);
                    level3SheetXssfCell.setCellValue("href");
                    level3SheetXssfCell = level3SheetRow.createCell(11);
                    level3SheetXssfCell.setCellValue("matchId");
                    level3SheetXssfCell = level3SheetRow.createCell(12);
                    level3SheetXssfCell.setCellValue("id");

                    for (int i = 0; i < rounds.size(); i++) {

                        XSSFRow xssfRow = level3Sheet.createRow(i + 1);
                        XSSFCell xssfCell = xssfRow.createCell(0);
                        xssfCell.setCellValue(tournamentSeason);
                        xssfCell = xssfRow.createCell(1);
                        xssfCell.setCellValue(tournamentName);
                        xssfCell = xssfRow.createCell(2);
                        xssfCell.setCellValue(tournamentStatus);
                        xssfCell = xssfRow.createCell(3);
                        xssfCell.setCellValue(tournamentDates);
                        xssfCell = xssfRow.createCell(10);
                        xssfCell.setCellValue(param.getString("tournamentHref"));
                        xssfCell = xssfRow.createCell(12);
                        xssfCell.setCellValue(tournamentId);

                        JsonObject roundDetail = new JsonObject();
                        Element element = rounds.get(i);
                        Integer length = element.childrenSize();
                        String matchId = element.attr("data-match-id");
                        roundDetail.put("matchId", matchId);
                        xssfCell = xssfRow.createCell(11);
                        xssfCell.setCellValue(matchId);
                        element = element.child(0);
                        String roundName = element.child(0).text();
                        roundDetail.put("roundName", roundName);
                        xssfCell = xssfRow.createCell(4);
                        xssfCell.setCellValue(roundName);
                        Element matchResultElemnet = element.child(1);

                        String player1name = matchResultElemnet.child(0).text();
                        roundDetail.put("player1name", player1name);
                        xssfCell = xssfRow.createCell(5);
                        xssfCell.setCellValue(player1name);
                        String player1score = matchResultElemnet.child(1).child(0).text();
                        roundDetail.put("player1score", player1score);
                        xssfCell = xssfRow.createCell(6);
                        xssfCell.setCellValue(player1score);
                        String all0score = matchResultElemnet.child(1).child(1).text();
                        roundDetail.put("all0score", all0score);
                        xssfCell = xssfRow.createCell(7);
                        xssfCell.setCellValue(all0score);
                        String player2score = matchResultElemnet.child(1).child(2).text();
                        roundDetail.put("player2score", player2score);
                        xssfCell = xssfRow.createCell(8);
                        xssfCell.setCellValue(player2score);
                        String player2name = matchResultElemnet.child(2).text();
                        roundDetail.put("player2name", player2name);
                        xssfCell = xssfRow.createCell(9);
                        xssfCell.setCellValue(player2name);
                        if (length > 2) {
                            String playedOnDate = element.child(3).child(1).text();
                            roundDetail.put("playedOnDate", playedOnDate);
                        }
                        if (length > 3) {
                            if (!matchResultElemnet.text().contains("Walkover")) {
                                if (element.child(4).text().contains("Referee")) {
                                    if (!param.getString("tournamentHref").contains("q-school")) {
                                        if (length > 4) {
                                            String frameScores = element.child(5).child(1).text();
                                            roundDetail.put("frameScores", frameScores);
                                        }
                                        if (length > 5) {
                                            String matchProgress = element.child(6).child(1).text();
                                            roundDetail.put("matchProgress", matchProgress);
                                        }
                                        if (length > 10) {
                                            String player1AvgShotTime = element.child(11).child(1).child(0).child(0).text();
                                            roundDetail.put("player1AvgShotTime", player1AvgShotTime);
                                            String player2AvgShotTime = element.child(11).child(1).child(0).child(1).text();
                                            roundDetail.put("player2AvgShotTime", player2AvgShotTime);
                                        }
                                    }
                                } else {
                                    if (!param.getString("tournamentHref").contains("q-school")) {
                                        if (length > 3) {
                                            String frameScores = element.child(4).child(1).text();
                                            roundDetail.put("frameScores", frameScores);
                                        }
                                        if (length > 4) {
                                            String matchProgress = element.child(5).child(1).text();
                                            roundDetail.put("matchProgress", matchProgress);
                                        }
                                        if (length > 9) {
                                            String player1AvgShotTime = element.child(10).child(1).child(0).child(0).text();
                                            roundDetail.put("player1AvgShotTime", player1AvgShotTime);
                                            String player2AvgShotTime = element.child(10).child(1).child(0).child(1).text();
                                            roundDetail.put("player2AvgShotTime", player2AvgShotTime);
                                        }
                                    }
                                }

                            }
                        }
                        round.add(roundDetail);
                    }

                    FileOutputStream fileOutputStream = null;
                    try {
                        File file = new File(tournamentName + "-" + tournamentId + ".xlsx");
                        if (file.exists()) {
                            file.delete();
                        }
                        file.createNewFile();

                        fileOutputStream = new FileOutputStream(file);
                        xssfWorkbook.write(fileOutputStream);
                        fileOutputStream.flush();
                        logger.info(tournamentName + ".xlsx，路径：{}", file.getAbsolutePath());
                    } finally {
                        if (null != fileOutputStream) {
                            try {
                                fileOutputStream.close();
                            } catch (Exception exception) {
                                logger.info("error:{}", exception);
                            }
                        }
                    }


                    tournament.put("match", round);
                    logger.info("{}", tournament);
                }
            } catch (Exception exception) {
                logger.error("error:{}", exception);
            }
            message.reply(result);
        });

        vertx.eventBus().consumer("classic.event.championshipLeague", message -> {
            JsonObject param = ((JsonObject) message.body());
            logger.info("param:{}", param);
            String result = RespBodyBuilder.toError(RespCode.CODE_10500);
            try {
                String data = commonRestService.getForObject(param.getString("tournamentHref"));
                JsonObject tournament = new JsonObject();
                tournament.put("tournamentHref", param.getString("tournamentHref"));
                String tournamentId = param.getString("tournamentHref").split("/")[6];
                tournament.put("tournamentId", tournamentId);
                if (!StringUtil.isEmpty(data)) {
                    Document document = Jsoup.parse(data);
//                    logger.info("{}", document.html());
                    String tournamentName = document.getElementsByClass("page-content").first().child(0).text();
                    tournament.put("tournamentName", tournamentName);
                    Elements tournamentDescs = document.getElementsByClass("page-content").first().child(2).child(0).getElementsByTag("tbody").first().children();
                    String tournamentStatus = tournamentDescs.get(0).child(1).text();
                    tournament.put("tournamentStatus", tournamentStatus);
                    String tournamentSeason = tournamentDescs.get(1).child(1).text();
                    tournament.put("tournamentSeason", tournamentSeason);
                    String tournamentDates = tournamentDescs.get(2).child(1).text();
                    tournament.put("tournamentDates", tournamentDates);

                    JsonArray round = new JsonArray();
                    Elements rounds = document.getElementsByClass("page-content").first().child(2).child(1).getElementsByClass("match");

                    XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
                    XSSFSheet level3Sheet = xssfWorkbook.createSheet(tournamentName);
                    XSSFRow level3SheetRow = level3Sheet.createRow(0);
                    XSSFCell level3SheetXssfCell = level3SheetRow.createCell(0);
                    level3SheetXssfCell.setCellValue("season");
                    level3SheetXssfCell = level3SheetRow.createCell(1);
                    level3SheetXssfCell.setCellValue("name");
                    level3SheetXssfCell = level3SheetRow.createCell(2);
                    level3SheetXssfCell.setCellValue("status");
                    level3SheetXssfCell = level3SheetRow.createCell(3);
                    level3SheetXssfCell.setCellValue("date");
                    level3SheetXssfCell = level3SheetRow.createCell(4);
                    level3SheetXssfCell.setCellValue("round-name");
                    level3SheetXssfCell = level3SheetRow.createCell(5);
                    level3SheetXssfCell.setCellValue("player1name");
                    level3SheetXssfCell = level3SheetRow.createCell(6);
                    level3SheetXssfCell.setCellValue("player1score");
                    level3SheetXssfCell = level3SheetRow.createCell(7);
                    level3SheetXssfCell.setCellValue("all0score");
                    level3SheetXssfCell = level3SheetRow.createCell(8);
                    level3SheetXssfCell.setCellValue("player2score");
                    level3SheetXssfCell = level3SheetRow.createCell(9);
                    level3SheetXssfCell.setCellValue("player2name");
                    level3SheetXssfCell = level3SheetRow.createCell(10);
                    level3SheetXssfCell.setCellValue("href");
                    level3SheetXssfCell = level3SheetRow.createCell(11);
                    level3SheetXssfCell.setCellValue("matchId");
                    level3SheetXssfCell = level3SheetRow.createCell(12);
                    level3SheetXssfCell.setCellValue("id");

                    for (int i = 0; i < rounds.size(); i++) {

                        XSSFRow xssfRow = level3Sheet.createRow(i + 1);
                        XSSFCell xssfCell = xssfRow.createCell(0);
                        xssfCell.setCellValue(tournamentSeason);
                        xssfCell = xssfRow.createCell(1);
                        xssfCell.setCellValue(tournamentName);
                        xssfCell = xssfRow.createCell(2);
                        xssfCell.setCellValue(tournamentStatus);
                        xssfCell = xssfRow.createCell(3);
                        xssfCell.setCellValue(tournamentDates);
                        xssfCell = xssfRow.createCell(10);
                        xssfCell.setCellValue(param.getString("tournamentHref"));
                        xssfCell = xssfRow.createCell(12);
                        xssfCell.setCellValue(tournamentId);

                        JsonObject roundDetail = new JsonObject();
                        Element element = rounds.get(i);
                        Integer length = element.childrenSize();
                        String matchId = element.attr("data-match-id");
                        roundDetail.put("matchId", matchId);
                        xssfCell = xssfRow.createCell(11);
                        xssfCell.setCellValue(matchId);
                        element = element.child(0);
                        String roundName = element.child(0).text();
                        roundDetail.put("roundName", roundName);
                        xssfCell = xssfRow.createCell(4);
                        xssfCell.setCellValue(roundName);
                        Element matchResultElemnet = element.child(1);

                        String player1name = matchResultElemnet.child(0).text();
                        roundDetail.put("player1name", player1name);
                        xssfCell = xssfRow.createCell(5);
                        xssfCell.setCellValue(player1name);
                        String player1score = matchResultElemnet.child(1).child(0).text();
                        roundDetail.put("player1score", player1score);
                        xssfCell = xssfRow.createCell(6);
                        xssfCell.setCellValue(player1score);
                        String all0score = matchResultElemnet.child(1).child(1).text();
                        roundDetail.put("all0score", all0score);
                        xssfCell = xssfRow.createCell(7);
                        xssfCell.setCellValue(all0score);
                        String player2score = matchResultElemnet.child(1).child(2).text();
                        roundDetail.put("player2score", player2score);
                        xssfCell = xssfRow.createCell(8);
                        xssfCell.setCellValue(player2score);
                        String player2name = matchResultElemnet.child(2).text();
                        roundDetail.put("player2name", player2name);
                        xssfCell = xssfRow.createCell(9);
                        xssfCell.setCellValue(player2name);
                        if (length > 2) {
                            String playedOnDate = element.child(3).child(1).text();
                            roundDetail.put("playedOnDate", playedOnDate);
                        }
                        if (length > 3) {
                            if (!matchResultElemnet.text().contains("Walkover")) {
                                if (element.child(4).text().contains("Referee")) {
                                    if (!param.getString("tournamentHref").contains("q-school")) {
                                        if (length > 4) {
                                            String frameScores = element.child(5).child(1).text();
                                            roundDetail.put("frameScores", frameScores);
                                        }
                                        if (length > 5) {
                                            String matchProgress = element.child(6).child(1).text();
                                            roundDetail.put("matchProgress", matchProgress);
                                        }
                                        if (length > 10) {
                                            String player1AvgShotTime = element.child(11).child(1).child(0).child(0).text();
                                            roundDetail.put("player1AvgShotTime", player1AvgShotTime);
                                            String player2AvgShotTime = element.child(11).child(1).child(0).child(1).text();
                                            roundDetail.put("player2AvgShotTime", player2AvgShotTime);
                                        }
                                    }
                                } else {
                                    if (!param.getString("tournamentHref").contains("q-school")) {
                                        if (length > 3) {
                                            String frameScores = element.child(4).child(1).text();
                                            roundDetail.put("frameScores", frameScores);
                                        }
                                        if (length > 4) {
                                            String matchProgress = element.child(5).child(1).text();
                                            roundDetail.put("matchProgress", matchProgress);
                                        }
                                        if (length > 9) {
                                            String player1AvgShotTime = element.child(10).child(1).child(0).child(0).text();
                                            roundDetail.put("player1AvgShotTime", player1AvgShotTime);
                                            String player2AvgShotTime = element.child(10).child(1).child(0).child(1).text();
                                            roundDetail.put("player2AvgShotTime", player2AvgShotTime);
                                        }
                                    }
                                }

                            }
                        }
                        round.add(roundDetail);
                    }

                    FileOutputStream fileOutputStream = null;
                    try {
                        File file = new File(tournamentName + "-" + tournamentId + ".xlsx");
                        if (file.exists()) {
                            file.delete();
                        }
                        file.createNewFile();

                        fileOutputStream = new FileOutputStream(file);
                        xssfWorkbook.write(fileOutputStream);
                        fileOutputStream.flush();
                        logger.info(tournamentName + ".xlsx，路径：{}", file.getAbsolutePath());
                    } finally {
                        if (null != fileOutputStream) {
                            try {
                                fileOutputStream.close();
                            } catch (Exception exception) {
                                logger.info("error:{}", exception);
                            }
                        }
                    }


                    tournament.put("match", round);
                    logger.info("{}", tournament);
                }
            } catch (Exception exception) {
                logger.error("error:{}", exception);
            }
            message.reply(result);
        });

        vertx.eventBus().consumer("classic.event.test", message -> {
            JsonObject param = ((JsonObject) message.body());
            String result = RespBodyBuilder.toError(RespCode.CODE_10500);
            try {
                String data = commonRestService.getForObject("http://api.snooker.org/?p=1");
                if (!StringUtil.isEmpty(data)) {
                    result = RespBodyBuilder.toSuccessWithObject(new JsonArray(data).getJsonObject(0));
                }
            } catch (Exception exception) {
                logger.error("error:{}", exception);
            }
            message.reply(result);
        });

        vertx.eventBus().consumer("classic.event.seasons", message -> {
            JsonObject param = ((JsonObject) message.body());
            String result = RespBodyBuilder.toError(RespCode.CODE_10500);
            try {
                JsonArray jsonArray = new JsonArray();
                String data = commonRestService.getForObject("https://cuetracker.net/seasons");
                if (!StringUtil.isEmpty(data)) {
                    Document document = Jsoup.parse(data);
                    Element tbody = document.getElementsByTag("tbody").first();
                    XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
                    XSSFSheet level3Sheet = xssfWorkbook.createSheet("seasons");
                    XSSFRow level3SheetRow = level3Sheet.createRow(0);
                    XSSFCell level3SheetXssfCell = level3SheetRow.createCell(0);
                    level3SheetXssfCell.setCellValue("season");
                    level3SheetXssfCell = level3SheetRow.createCell(1);
                    level3SheetXssfCell.setCellValue("seasonHref");
                    level3SheetXssfCell = level3SheetRow.createCell(2);
                    level3SheetXssfCell.setCellValue("professional");
                    level3SheetXssfCell = level3SheetRow.createCell(3);
                    level3SheetXssfCell.setCellValue("nonProfessional");
                    level3SheetXssfCell = level3SheetRow.createCell(4);
                    level3SheetXssfCell.setCellValue("total");

                    Elements seasons = tbody.children();
                    for (int i = 0; i < seasons.size(); i++) {
                        JsonObject object = new JsonObject();
                        Element element = seasons.get(i);
                        String season = element.children().get(0).text();
                        String seasonHref = element.children().get(0).child(0).attr("href");
                        String professional = element.children().get(1).text();
                        String nonProfessional = element.children().get(2).text();
                        String total = element.children().get(3).text();
                        logger.info("{}:{}:{}:{}:{}", season, professional, nonProfessional, total, seasonHref);
                        XSSFRow xssfRow = level3Sheet.createRow(i + 1);
                        XSSFCell xssfCell = xssfRow.createCell(0);
                        xssfCell.setCellValue(season);
                        object.put("season", season);
                        xssfCell = xssfRow.createCell(1);
                        xssfCell.setCellValue(seasonHref);
                        object.put("seasonHref", seasonHref);
                        xssfCell = xssfRow.createCell(2);
                        xssfCell.setCellValue(professional);
                        object.put("professional", professional);
                        xssfCell = xssfRow.createCell(3);
                        xssfCell.setCellValue(nonProfessional);
                        object.put("nonProfessional", nonProfessional);
                        xssfCell = xssfRow.createCell(4);
                        xssfCell.setCellValue(total);
                        object.put("total", total);
                        jsonArray.add(object);
//                        vertx.eventBus().send("classic.event.season", new JsonObject().put("seasonHref", seasonHref));
                    }
                    FileOutputStream fileOutputStream = null;
                    try {
                        File file = new File("seasons.xlsx");
                        if (file.exists()) {
                            file.delete();
                        }
                        file.createNewFile();

                        fileOutputStream = new FileOutputStream(file);
                        xssfWorkbook.write(fileOutputStream);
                        fileOutputStream.flush();
                        logger.info("seasons.xlsx，路径：{}", file.getAbsolutePath());
                        result = RespBodyBuilder.toSuccessWithObject(jsonArray);
                    } finally {
                        if (null != fileOutputStream) {
                            try {
                                fileOutputStream.close();
                            } catch (Exception exception) {
                                logger.info("error:{}", exception);
                            }
                        }
                    }
                }
            } catch (Exception exception) {
                logger.error("error:{}", exception);
            }
            message.reply(result);
        });

        vertx.eventBus().consumer("classic.event.season", message -> {
            JsonObject param = ((JsonObject) message.body());
            logger.info("param:{}", param);
            String result = RespBodyBuilder.toError(RespCode.CODE_10500);
            try {
                JsonObject season = new JsonObject();
                season.put("seasonHref", param.getString("seasonHref"));
                String data = commonRestService.getForObject(param.getString("seasonHref"));
                JsonArray ts = new JsonArray();
                if (!StringUtil.isEmpty(data)) {
                    Document document = Jsoup.parse(data);
                    Element tbody = document.getElementsByTag("tbody").get(2);
                    Elements tournaments = tbody.children();
                    for (int i = 0; i < tournaments.size(); i++) {
                        JsonObject t = new JsonObject();
                        Element tournament = tournaments.get(i);
                        String tournamentDate = tournament.children().get(0).text();
                        t.put("tournamentDate", tournamentDate);
                        String tournamentName = tournament.children().get(1).text();
                        t.put("tournamentName", tournamentName);
                        String tournamentHref = tournament.children().get(1).getElementsByTag("a").attr("href");
                        t.put("tournamentHref", tournamentHref);
                        String tournamentType = tournament.children().get(2).text();
                        t.put("tournamentType", tournamentType);
                        String tournamentWinner = tournament.children().get(3).text();
                        t.put("tournamentWinner", tournamentWinner);
                        ts.add(t);
//                        logger.info("{}:{}:{}:{}:{}", tournamentDate, tournamentName, tournamentHref, tournamentType, tournamentWinner);
                        vertx.eventBus().send("classic.event.tournament", new JsonObject().put("tournamentHref", tournamentHref));
                    }
                }
//                logger.info("{}", ts);
            } catch (Exception exception) {
                logger.error("error:{}", exception);
            }
            message.reply(result);
        });

        vertx.eventBus().consumer("classic.event.tournament", message -> {
            JsonObject param = ((JsonObject) message.body());
            logger.info("param:{}", param);
            String result = RespBodyBuilder.toError(RespCode.CODE_10500);
            try {
                String data = commonRestService.getForObject(param.getString("tournamentHref"));
                JsonObject tournament = new JsonObject();
                tournament.put("tournamentHref", param.getString("tournamentHref"));
                if (!StringUtil.isEmpty(data)) {
                    Document document = Jsoup.parse(data);
//                    logger.info("{}", document.html());
                    String tournamentName = document.getElementsByClass("page-content").first().child(0).text();
                    tournament.put("tournamentName", tournamentName);
                    JsonArray round = new JsonArray();
                    Elements rounds = document.getElementsByClass("page-content").first().child(2).child(1).getElementsByClass("match");
                    for (int i = 0; i < rounds.size(); i++) {
                        JsonObject roundDetail = new JsonObject();
                        Element element = rounds.get(i);
                        Integer length = element.childrenSize();
                        String matchId = element.attr("data-match-id");
                        roundDetail.put("matchId", matchId);
                        element = element.child(0);
                        String roundName = element.child(0).text();
                        roundDetail.put("roundName", roundName);
                        Element matchResultElemnet = element.child(1);

                        String player1name = matchResultElemnet.child(0).text();
                        roundDetail.put("player1name", player1name);
                        String player1score = matchResultElemnet.child(1).child(0).text();
                        roundDetail.put("player1score", player1score);
                        String all0score = matchResultElemnet.child(1).child(1).text();
                        roundDetail.put("all0score", all0score);
                        String player2score = matchResultElemnet.child(1).child(2).text();
                        roundDetail.put("player2score", player2score);
                        String player2name = matchResultElemnet.child(2).text();
                        roundDetail.put("player2name", player2name);
                        if (length > 2) {
                            String playedOnDate = element.child(3).child(1).text();
                            roundDetail.put("playedOnDate", playedOnDate);
                        }
                        if (length > 3) {
                            if (!matchResultElemnet.text().contains("Walkover")) {
                                if (element.child(4).text().contains("Referee")) {
                                    if (!param.getString("tournamentHref").contains("q-school")) {
                                        if (length > 4) {
                                            String frameScores = element.child(5).child(1).text();
                                            roundDetail.put("frameScores", frameScores);
                                        }
                                        if (length > 5) {
                                            String matchProgress = element.child(6).child(1).text();
                                            roundDetail.put("matchProgress", matchProgress);
                                        }
                                        if (length > 10) {
                                            String player1AvgShotTime = element.child(11).child(1).child(0).child(0).text();
                                            roundDetail.put("player1AvgShotTime", player1AvgShotTime);
                                            String player2AvgShotTime = element.child(11).child(1).child(0).child(1).text();
                                            roundDetail.put("player2AvgShotTime", player2AvgShotTime);
                                        }
                                    }
                                } else {
                                    if (!param.getString("tournamentHref").contains("q-school")) {
                                        if (length > 3) {
                                            String frameScores = element.child(4).child(1).text();
                                            roundDetail.put("frameScores", frameScores);
                                        }
                                        if (length > 4) {
                                            String matchProgress = element.child(5).child(1).text();
                                            roundDetail.put("matchProgress", matchProgress);
                                        }
                                        if (length > 9) {
                                            String player1AvgShotTime = element.child(10).child(1).child(0).child(0).text();
                                            roundDetail.put("player1AvgShotTime", player1AvgShotTime);
                                            String player2AvgShotTime = element.child(10).child(1).child(0).child(1).text();
                                            roundDetail.put("player2AvgShotTime", player2AvgShotTime);
                                        }
                                    }
                                }

                            }
                        }
                        round.add(roundDetail);
                    }
                    tournament.put("match", round);
                    logger.info("{}", tournament);
                }
            } catch (Exception exception) {
                logger.error("error:{}", exception);
            }
            message.reply(result);
        });
    }
}
